Skip to content

Latest commit

 

History

History
254 lines (196 loc) · 7.9 KB

1. Introduction to C++.md

File metadata and controls

254 lines (196 loc) · 7.9 KB

1. Introduction to C++


What is C++?

  • C++ is a cross-platform language that can be used to create high-performance applications.

  • C++ was developed by Bjarne Stroustrup, as an extension to the C language.

  • C++ gives programmers a high level of control over system resources and memory.

  • The language was updated 4 major times in 2011, 2014, 2017, and 2020 to C++11, C++14, C++17, C++20.

  • C++ is a hybrid object-oriented programming language, as it's based on C which is purely a procedural language.

  • C++ is a multi-paradigm programming language, which means it can be procedural, functional, object oriented, generic and modular programming language depending on its application.

  • C++ is a middle level programming language which means it supports both low-level programming (drivers, kernels) and high-level (games, GUI, desktop apps etc) programming.

  • C++ is a case-sensitive language.


C vs C++

Points C C++
Founder C was developed by Dennis Ritchie between the years 1969 and 1973 at AT&T Bell Labs. C++ was developed by Bjarne Stroustrup in 1979.
Programming Paradigms Structured and Procedural Procedural, Object Oriented, Functional, Generic and Modular
Keywords Has 32 Keywords Has 52 Keywords
Superset / Subset C is the SUBSET of C++ C++ is the SUPERSET of C
Approach It follows Top - Down approach It follows Bottom - Up approach
Data and Functions Data and Function are separated Data and Function are encapsulated to form an object
Driven Type It is a Function Driven Language It is a Object Driven Language
Input & Output scanf() is used as input function whereas printf() is used as output function cin is used as input function whereas cout is used as output function

Since both the languages are too vast, the list of their differences cannot be specified. So it's better to learn both the languages to get more clarity about their differences.


'Hello World' in C++

#include <iostream>
using namespace std;

int main()
{
	cout << "hello world";
	return 0;
}

// Output: Hello World

Explanation of the above code:

  • Line 1: #include<iostream>

    #’ indicates that the following line is a preprocessor directive and should be processed by the preprocessor before compilation by the compiler.

    So, ‘#include’ is a preprocessor directive that tells the pre-processor to include header files in the program.

    < >‘ indicates the start and end of the file name to be included.

    iostream’ is a header file that contains functions for input/output operations (‘cin’ and ‘cout’).

    Now to sum it up, C++ to English translation of the command, #include<iostream> is:
    Dear pre-processor, please include all the contents of the header file iostream at the very beginning of this program before the compiler starts the actual compilation of the code.

    There are two ways to write the main function:

    1. int main(){} - will return a value
     #include <iostream>
     using namespace std;
    
     int main()
     {
     	cout << "hello world";
     	return 0;
     }
    
    1. void main(){} - will not return a value
     #include <iostream>
     using namespace std;
    
     void main()
     {
     	cout << "hello world";
     }
    

    The difference between void main() and int main() is that the void main() indicates that the main() function will not return any value, but the int main() indicates that the main() can return integer type data. When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main().

    NOTE:
    We will discuss the concept of "Functions" later in the chapter "Functions". And we will also learn more about “Header Files” later in the chapter “Pre-processor Directive”
  • Line 2: using namespace std;

    using namespace std; means that we can use names for objects and variables from the standard library.

    Don't worry if you don't understand how #include<iostream> and using namespace std; works. Just think of it as something that (almost) always appears at the start in your program when you are coding in C++.

  • Line 3: A Blank Line

    C++ ignores white space.

    Here, (in the above example) I have shown a lot of blank lines by using the keyboard button “Enter”. The code can also be written as (because we know that the C++ ignores white spaces. But by using blank lines and white spaces the code is more readable):

    #include <iostream>;using namespace std;int main(){cout << "hello world";return 0;}
    
    NOTE:
    Don’t add too much blank space in your code.
  • Line 4: int main() This is a function, called the main function. Any code inside its curly brackets {} will be executed.There must be only one main function in a C++ program, and it must always return a number of the int type.

  • Line 5: cout (pronounced "see-out")

    It is an object used together with the insertion operator (<<) to output/print text. In our example it will output "Hello World".

    NOTE:
    Every C++ statement ends with a semicolon ';'
  • Line 6: return 0 ends the main function.

  • Line 7: Do not forget to add the closing curly bracket '}' to actually end the main function.

    NOTE:
    Must read this article from GeeksForGeeks - “Why 'using namespace std;' is considered bad practice”. Yes it is true that using namespace std is a bad practice, but this problem only occurs when you are an advanced C++ coder.

    Therefore, according to the above "Note", the code can be written as:

    #include <iostream>
    int main() {
        std::cout <<pre "hello world";
        return 0;
    }
    

Comments

#include <iostream>
using namespace std;

// single line comment

int main() {
    cout <<pre "hello world"; // single line comment
    return 0;

    /*
    Multi Line Comment
    Multi Line Comment
    */
}